package cmw.com.beijingnew.fragment;

import android.support.annotation.IdRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cmw.com.base.BaseFragment;
import cmw.com.base.BasePager;
import cmw.com.base.MenuDetailBasePager;
import cmw.com.beijingnew.R;
import cmw.com.beijingnew.activity.MainActivity;
import cmw.com.beijingnew.constant.SysConstant;
import cmw.com.beijingnew.pager.GovaffairPager;
import cmw.com.beijingnew.pager.HomePager;
import cmw.com.beijingnew.pager.NewsCenterPager;
import cmw.com.beijingnew.pager.SettingPager;
import cmw.com.beijingnew.pager.SmartServicePager;
import cmw.com.beijingnew.view.NoScrollViewPager;
import cmw.com.utils.SystemUtil;

/**
 * 主页面内容UI Fragment
 * Created by Administrator on 2017/4/3.
 */

public class ContentFragment extends BaseFragment {
    @ViewInject(R.id.rg_bottom_groups)
    private RadioGroup rg_bottom_groups;

    @ViewInject(R.id.ll_contentMain)
    private LinearLayout ll_contentMain;

    @ViewInject(R.id.viewpager)
    private NoScrollViewPager viewpager;

    List<BasePager> basePagers = new ArrayList<BasePager>();



    @Override
    public View initView() {
       final View view = View.inflate(context, R.layout.content_fragment, null);
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                resizeRadioGroup(view);
            }
        });
        return view;
    }

    /**
     * 调整 RadioGroup Padding
     * 在底部有虚拟键时会档住底部导航菜单
     * @param view
     */
    private void resizeRadioGroup(final View view){
        LogUtils.i("onGlobalLayout 执行了哟....");
        boolean flag = SystemUtil.isScreenChange(context);
        boolean isNB = SystemUtil.checkDeviceHasNavigationBar(context);
        if(flag || !isNB){//横屏或没有虚拟按键时，将竖屏中设置的 50dp 设为 0
          //  int px = DensityUtil.dip2px(context,50);
            ll_contentMain.setPadding(0,0,0,0);
        }else{
        }
    }

    @Override
    public void initData() {
        super.initData();
        basePagers.add(new HomePager(context));
        basePagers.add(new NewsCenterPager(context));
        basePagers.add(new SmartServicePager(context));
        basePagers.add(new GovaffairPager(context));
        basePagers.add(new SettingPager(context));
        viewpager.setAdapter(new ContentFragmentAdapter());
        rg_bottom_groups.check(R.id.rb_home);
        rg_bottom_groups.setOnCheckedChangeListener(new RgOnCheckedChangeListener());
        viewpager.addOnPageChangeListener(new VwOnPageChangeListener());
        /*页面初始化完成，就默认加载首页数据*/
        basePagers.get(0).initData();
         /*页面初始化完成，就默认隐藏SlidingMenu,使其不可以滑动*/
        enabledSlidingMenu(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

    }

    public BasePager getNewsCenterPager(){
        return basePagers.get(1);
    }

    /**
     * ViewPager 页面选中事件
     */
    class VwOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        /**
         * 在当前ViewPager选中时加载初始化数据，
         * 避免每次点击时都去加载，节省用户流量
         * @param position
         */
        @Override
        public void onPageSelected(int position) {
            basePagers.get(position).initData();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    /**
     * 底部RadioGroup 选中事件
     */
    class RgOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            switch (i){
                case R.id.rb_home :
                    viewpager.setCurrentItem(0,false); //设置当前显示的是首页，false 取消动画
                    enabledSlidingMenu(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                    break;
                case R.id.rb_news :
                    viewpager.setCurrentItem(1,false);//设置当前显示的是新闻，false 取消动画
                    enabledSlidingMenu(DrawerLayout.LOCK_MODE_UNLOCKED);
                    break;
                case R.id.rb_zhihui :
                    viewpager.setCurrentItem(2,false);//设置当前显示的是智慧，false 取消动画
                    enabledSlidingMenu(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                    break;
                case R.id.rb_zhenyao :
                    viewpager.setCurrentItem(3,false);//设置当前显示的是政要，false 取消动画
                    enabledSlidingMenu(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                    break;
                case R.id.rb_setting :
                    viewpager.setCurrentItem(4,false);//设置当前显示的是设置，false 取消动画
                    enabledSlidingMenu(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                    break;
            }
        }
    }

    /**
     * 显示或隐藏 SlidingMenu
     * @param ops
     */
    private void enabledSlidingMenu(int ops){
        MainActivity mainActivity = (MainActivity)context;
        mainActivity.setDrawerLockMode(ops);
    }

    /**
     * ViewPager 适配器
     */
    class ContentFragmentAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return basePagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = basePagers.get(position);
            View rootView = pager.rootView;
          //  pager.initData();
            container.addView(rootView);
            return rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
